﻿using DTcms.Common;
using DTcms.Model;
using DTcms.Web.Mvc.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web.Mvc;

namespace DTcms.Web.Mvc.Areas.Admin.Controllers
{
    public class UsersController : ManageController
    {
        protected BaseApi _service = new BaseApi();
        protected int totalCount = 0;
        protected Model.manager managerModel = new Model.manager();
        protected string _action;
        protected int _id;
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            managerModel = GetAdminInfo();
            _action = DTRequest.GetQueryString("action");
            _id = DTRequest.GetQueryInt("id");
            ViewBag.action = _action;
            ViewBag.id = _id;
        }

        #region Oauth授权
        [ChkAdminScript("user_oauth", DTEnums.ActionEnum.View)]
        public ActionResult site_oauth_list()
        {
            return View();
        }

        #region 组合SQL查询语句==========================
        protected string SiteOauthSqlTxt(string _keywords, int _site_id)
        {
            StringBuilder strTemp = new StringBuilder();
            if (_site_id > 0)
            {
                strTemp.Append(" and site_id=" + _site_id);
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and title like  '%" + _keywords + "%'");
            }
            return strTemp.ToString();
        }
        #endregion
        #region 数据绑定
        [ChkAdminJson("user_oauth", DTEnums.ActionEnum.View)]
        public JsonResult SiteOauthList(string keywords, int site_id = 0, int page = 1, int limit = 10)
        {
            string strWhere = "id > 0" + SiteOauthSqlTxt(keywords, site_id);
            string strOrder = "sort_id asc,id desc";
            DataTable resultList = _service.SiteOauthBind(strWhere, strOrder, out totalCount, page, limit);
            DataTable oldData = resultList.Clone();
            if (totalCount > 0)
            {
                oldData.Columns.Add("siteTitle", Type.GetType("System.String"));
                foreach (DataRow item in resultList.Rows)
                {
                    DataRow row = oldData.NewRow();
                    row["title"] = item["title"];
                    row["ptitle"] = item["ptitle"].ToString(); ;
                    row["add_time"] = item["add_time"];
                    row["img_url"] = item["img_url"].ToString(); ;
                    row["is_lock"] = item["is_lock"];
                    row["sort_id"] = item["sort_id"];
                    row["id"] = item["id"];
                    row["siteTitle"] = new BLL.sites().GetTitle(Utils.ObjToInt(item["site_id"]));
                    oldData.Rows.Add(row);
                }
            }
            return Json(new { status = 1, msg = "", item = oldData, totalCount = totalCount });
        }
        #endregion

        [HttpPost]
        [ChkAdminJson("user_group", DTEnums.ActionEnum.Edit)]
        public JsonResult GroupFiledSave(string field, string value, int id = 0)
        {
            if (string.IsNullOrEmpty(field) || id == 0)
            {
                return Json(new { status = 0, msg = "参数错误！" });
            }
            BLL.user_groups bll = new BLL.user_groups();
            if (bll.UpdateFile("id=" + id, string.Format("{0}='{1}'", field, value)))
            {
                return Json(new { status = 1, msg = "修改成功！" });
            }
            return Json(new { status = 0, msg = "修改失败！" });
        }

        [HttpPost]
        [ChkAdminJson("user_oauth", DTEnums.ActionEnum.Edit)]
        public JsonResult SiteOauthFiledSave(string field, int value, int id = 0)
        {
            if (string.IsNullOrEmpty(field) || id == 0)
            {
                return Json(new { status = 0, msg = "参数错误！" });
            }
            BLL.site_oauth bll = new BLL.site_oauth();
            if (bll.UpdateFile("id=" + id, field + "=" + value))
            {
                return Json(new { status = 1, msg = "修改成功！" });
            }
            return Json(new { status = 0, msg = "修改失败！" });
        }

        [HttpPost]
        [ChkAdminJson("user_oauth", DTEnums.ActionEnum.Delete)]
        public JsonResult SiteOauthDelete(List<int> ids)
        {
            return _service.SiteOauthDelete(ids);
        }

        [ChkAdminScript("user_oauth", DTEnums.ActionEnum.View)]
        public ActionResult site_oauth_edit()
        {
            Model.site_oauth model = new Model.site_oauth();
            int site_id = DTRequest.GetQueryInt("site_id");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                _action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (_id == 0)
                {
                    return ResultMsg("传输参数不正确！", "back");
                }
                model = _service.SiteOauthinfo(_id);
                if (model == null)
                {
                    return ResultMsg("记录不存在或已被删除！", "back");
                }
            }
            return View(model);
        }
        [ChkAdminScript("user_oauth", DTEnums.ActionEnum.View)]
        public ActionResult Oauth_App_Edit()
        {
            Model.oauth_app model = new Model.oauth_app();
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                _action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (_id == 0)
                {
                    return ResultMsg("传输参数不正确！", "back");
                }
                model = _service.OauthAppinfo(_id);
                if (model == null)
                {
                    return ResultMsg("记录不存在或已被删除！", "back");
                }
            }
            return View(model);
        }
        [HttpPost, CustomValidateAntiForgeryToken]
        public ActionResult SiteOauthSave(Model.site_oauth t)
        {
            SaveModel save = _service.SiteOauthSave(_action, _id, t);
            save.url = save.status == 0 ? "back" : "../users/site_oauth_list";
            return Json(save);
        }
        [HttpPost, CustomValidateAntiForgeryToken]
        public ActionResult OauthAppSave(Model.oauth_app t)
        {
            SaveModel save = _service.OauthAppSave(_action, _id, t);
            save.url = save.status == 0 ? "back" : "../users/oauth_app_list";
            return Json(save);
        }
        #endregion
        #region 消费记录
        [ChkAdminScript("user_amount_log", DTEnums.ActionEnum.View)]
        public ActionResult Amount_Log()
        {
            return View();
        }
        #region 组合SQL查询语句==========================
        protected string AmountLogCombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and (user_name='" + _keywords + "' or remark like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion
        #region 数据绑定
        [ChkAdminScript("user_amount_log", DTEnums.ActionEnum.View)]
        public JsonResult AmountLogList(string keywords, int page = 1, int limit = 10)
        {
            string strWhere = "id > 0" + AmountLogCombSqlTxt(keywords);
            string strOrder = "add_time desc,id desc";
            var resultList = _service.AmountLogBind(strWhere, strOrder, page, limit);
            return Json(new { status = 1, msg = "", item = resultList.Items, totalCount = resultList.TotalItems });
        }
        #endregion
        #region 删除
        [HttpPost]
        [ChkAdminJson("user_amount_log", DTEnums.ActionEnum.Delete)]
        public JsonResult AmountLogDelete(List<int> ids)
        {
            return _service.AmountLogDelete(ids);
        }
        #endregion
        #endregion
        #region 会员组管理
        [ChkAdminScript("user_group", DTEnums.ActionEnum.View)]
        public ActionResult Group_List()
        {
            return View();
        }
        #region 组合SQL查询语句==========================
        protected string GroupCombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and title like '%" + _keywords + "%'");
            }
            return strTemp.ToString();
        }
        #endregion
        #region 数据绑定
        [ChkAdminScript("user_group", DTEnums.ActionEnum.View)]
        public JsonResult GroupList(string keywords)
        {
            string strWhere = "id > 0" + GroupCombSqlTxt(keywords);
            string strOrder = "grade asc,id asc";
            var resultList = new BLL.user_groups().GetModelList(0, strWhere, "", strOrder);
            return Json(new { status = 1, msg = "", item = resultList });
        }
        #endregion
        #region 删除
        [HttpPost]
        [ChkAdminJson("user_group", DTEnums.ActionEnum.Delete)]
        public JsonResult GroupDelete(List<int> ids)
        {
            return _service.GroupDelete(ids);
        }
        #endregion
        #region 修改会员组状态
        [HttpPost]
        [ChkAdminJson("user_group", DTEnums.ActionEnum.Edit)]
        public JsonResult GroupLock(int id, bool is_lock)
        {
            BLL.user_groups bll = new BLL.user_groups();
            if (bll.UpdateFile("id=" + id, "is_lock=" + (is_lock ? 0 : 1)))
            {
                return Json(new { status = 1, msg = "状态修改成功！" });
            }
            return Json(new { status = 0, msg = "状态修改失败！" });
        }
        [HttpPost]
        [ChkAdminJson("user_oauth", DTEnums.ActionEnum.Edit)]
        public JsonResult OauthAppLock(int id, bool is_lock)
        {
            BLL.oauth_app bll = new BLL.oauth_app();
            if (bll.UpdateFile("id=" + id, "is_lock=" + (is_lock ? 0 : 1)))
            {
                return Json(new { status = 1, msg = "状态修改成功！" });
            }
            return Json(new { status = 0, msg = "状态修改失败！" });
        }
        #endregion
        #region 编辑、添加
        [ChkAdminScript("user_group", DTEnums.ActionEnum.View)]
        public ActionResult Group_Edit()
        {
            Model.user_groups model = new Model.user_groups();
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                _action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (_id == 0)
                {
                    return ResultMsg("传输参数不正确！", "back");
                }
                model = _service.GroupInfo(_id);
                if (model == null)
                {
                    return ResultMsg("记录不存在或已被删除！", "back");
                }
            }
            return View(model);
        }

        [HttpPost, CustomValidateAntiForgeryToken]
        public ActionResult GroupSave(Model.user_groups t)
        {
            SaveModel save = _service.GroupSave(_action, _id, t);
            save.url = save.status == 0 ? "back" : "../users/group_list";
            return Json(save);
        }
        #endregion
        #endregion
        #region 邮件模板
        [ChkAdminScript("user_mail_template", DTEnums.ActionEnum.View)]
        public ActionResult Mail_Template_List()
        {
            return View();
        }

        #region 组合SQL查询语句==========================
        protected string MailCombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and (title like  '%" + _keywords + "%' or mail_title like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion
        #region 数据绑定
        [ChkAdminJson("user_mail_template", DTEnums.ActionEnum.View)]
        public JsonResult MailList(string keywords, int page = 1, int limit = 10)
        {
            string strWhere = "id > 0" + MailCombSqlTxt(keywords);
            string strOrder = "id asc";
            var resultList = _service.MailBind(strWhere, strOrder, page, limit);
            return Json(new { status = 1, msg = "", item = resultList.Items, totalCount = resultList.TotalItems });
        }
        #endregion

        #region 删除
        [HttpPost]
        [ChkAdminJson("user_mail_template", DTEnums.ActionEnum.Delete)]
        public JsonResult MailDelete(List<int> ids)
        {
            return _service.MailDelete(ids);
        }
        #endregion

        #region 编辑、添加
        [ChkAdminScript("user_mail_template", DTEnums.ActionEnum.View)]
        public ActionResult Mail_Template_Edit()
        {
            Model.mail_template model = new Model.mail_template();
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                _action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (_id == 0)
                {
                    return ResultMsg("传输参数不正确！", "back");
                }
                model = _service.MailInfo(_id);
                if (model == null)
                {
                    return ResultMsg("记录不存在或已被删除！", "back");
                }
            }
            return View(model);
        }

        [HttpPost, ValidateInput(false), CustomValidateAntiForgeryToken]
        public ActionResult MailSave(Model.mail_template t)
        {
            SaveModel save = _service.MailSave(_action, _id, t);
            save.url = save.status == 0 ? "back" : "../users/mail_template_list";
            return Json(save);
        }
        #endregion
        #endregion
        #region 站内短消息
        [ChkAdminScript("user_message", DTEnums.ActionEnum.View)]
        public ActionResult Message_List()
        {
            return View();
        }

        #region 组合SQL查询语句==========================
        protected string MessageSqlTxt(string _keywords, int _type_id)
        {
            StringBuilder strTemp = new StringBuilder();
            if (_type_id > 0)
            {
                strTemp.Append(" and type=" + _type_id);
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and (accept_user_name='" + _keywords + "' or post_user_name like '%" + _keywords + "%' or title like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion
        #region 数据绑定
        [ChkAdminJson("user_message", DTEnums.ActionEnum.View)]
        public JsonResult MessageList(string keywords, int type_id = 0, int page = 1, int limit = 10)
        {
            string strWhere = "id > 0" + MessageSqlTxt(keywords, type_id);
            string strOrder = "post_time desc,id desc";
            var resultList = _service.MessageBind(strWhere, strOrder, page, limit);
            return Json(new { status = 1, msg = "", item = resultList.Items, totalCount = resultList.TotalItems });
        }
        #endregion

        #region 删除
        [HttpPost]
        [ChkAdminJson("user_message", DTEnums.ActionEnum.Delete)]
        public JsonResult MessageDelete(List<int> ids)
        {
            return _service.MessageDelete(ids);
        }
        #endregion

        #region 编辑、添加
        [ChkAdminScript("user_message", DTEnums.ActionEnum.View)]
        public ActionResult Message_Edit()
        {
            Model.user_message model = new Model.user_message();
            ViewBag.View = false;
            ViewBag.Add = true;
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.View.ToString())
            {
                if (_id == 0)
                {
                    return ResultMsg("传输参数不正确！", "back");
                }
                model = _service.MessageInfo(_id);
                if (model == null)
                {
                    return ResultMsg("记录不存在或已被删除！", "back");
                }
                ViewBag.View = true;
                ViewBag.Add = false;
            }
            return View(model);
        }

        [HttpPost, ValidateInput(false), CustomValidateAntiForgeryToken]
        [ChkAdminScript("user_message", DTEnums.ActionEnum.Add)]
        public ActionResult MessageSave(Model.user_message t)
        {
            SaveModel save = _service.MessageSave(t);
            save.url = save.status == 0 ? "back" : "../users/message_list";
            return Json(save);
        }
        #endregion
        #endregion
        #region OAuth
        #region OAuth授权列表
        [ChkAdminScript("user_oauth", DTEnums.ActionEnum.View)]
        public ActionResult User_Oauth()
        {
            return View();
        }
        #region 数据绑定
        #region 组合SQL查询语句==========================
        protected string OauthSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and (user_name like '%" + _keywords + "%' or oauth_name like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion
        [ChkAdminScript("user_oauth", DTEnums.ActionEnum.View)]
        public JsonResult OauthList(string keywords, int page = 1, int limit = 10)
        {
            string strWhere = "id > 0" + OauthSqlTxt(keywords);
            string strOrder = "add_time desc,id desc";
            var resultList = _service.OauthBind(strWhere, strOrder, page, limit);
            return Json(new { status = 1, msg = "", item = resultList.Items, totalCount = resultList.TotalItems });
        }
        #endregion

        #region 数据绑定
        #region 组合SQL查询语句==========================
        protected string OauthAppSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and title like  '%" + _keywords + "%'");
            }
            return strTemp.ToString();
        }
        #endregion
        [ChkAdminScript("user_oauth", DTEnums.ActionEnum.View)]
        public JsonResult OauthAppList(string keywords, int page = 1, int limit = 10)
        {
            string strWhere = "id > 0" + OauthAppSqlTxt(keywords);
            string strOrder = "sort_id asc,id desc";
            var resultList = _service.OauthAppBind(strWhere, strOrder, page, limit);
            return Json(new { status = 1, msg = "", item = resultList.Items, totalCount = resultList.TotalItems });
        }
        #endregion

        #region 删除
        [HttpPost]
        [ChkAdminJson("user_oauth", DTEnums.ActionEnum.Delete)]
        public JsonResult OauthDelete(List<int> ids)
        {
            return _service.OauthDelete(ids);
        }
        #endregion
        #endregion
        #region OAuth应用
        [ChkAdminScript("user_oauth", DTEnums.ActionEnum.View)]
        public ActionResult Oauth_App_List()
        {
            return View();
        }
        #region 组合SQL查询语句==========================
        protected string OauthAppTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and title like  '%" + _keywords + "%'");
            }
            return strTemp.ToString();
        }
        #endregion
        #region 删除
        [HttpPost]
        [ChkAdminJson("user_oauth", DTEnums.ActionEnum.Delete)]
        public JsonResult OauthAppDelete(List<int> ids)
        {
            return _service.OauthAppDelete(ids);
        }
        #endregion
        [HttpPost]
        #endregion
        #endregion
        #region 积分记录
        [ChkAdminScript("user_point_log", DTEnums.ActionEnum.View)]
        public ActionResult Point_Log()
        {
            return View();
        }
        [ChkAdminScript("user_point_log", DTEnums.ActionEnum.View)]
        public ActionResult Point_List()
        {
            return View();
        }
        #region 组合SQL查询语句==========================
        protected string PointLogSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and (user_name='" + _keywords + "' or remark like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion
        #region 数据绑定
        [ChkAdminJson("user_point_log", DTEnums.ActionEnum.View)]
        public JsonResult PointLogList(string keywords, int page = 1, int limit = 10)
        {
            string strWhere = "id > 0" + PointLogSqlTxt(keywords);
            string strOrder = "add_time desc,id desc";
            var resultList = _service.PointLogBind(strWhere, strOrder, page, limit);
            return Json(new { status = 1, msg = "", item = resultList.Items, totalCount = resultList.TotalItems });
        }
        #endregion

        #region 删除
        [HttpPost]
        [ChkAdminJson("user_point_log", DTEnums.ActionEnum.Delete)]
        public JsonResult PointLogDelete(List<int> ids)
        {
            return _service.PointLogDelete(ids);
        }
        #endregion

        #endregion
        #region 充值记录
        [ChkAdminScript("user_recharge_log", DTEnums.ActionEnum.View)]
        public ActionResult Recharge_List()
        {
            return View();
        }
        #region 组合SQL查询语句==========================
        protected string RechargeSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and (user_name='" + _keywords + "' or recharge_no like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion
        #region 数据绑定
        [ChkAdminScript("user_recharge_log", DTEnums.ActionEnum.View)]
        public JsonResult RechargeList(string keywords, int page = 1, int limit = 10)
        {
            string strWhere = "id > 0" + RechargeSqlTxt(keywords);
            string strOrder = "add_time desc,id desc";
            var resultList = _service.RechargeBind(strWhere, strOrder, page, limit);
            DataTable oldData = new DataTable();
            oldData.Columns.Add("id", Type.GetType("System.Int32"));
            oldData.Columns.Add("user_id", Type.GetType("System.Int32"));
            oldData.Columns.Add("status", Type.GetType("System.Int32"));
            oldData.Columns.Add("user_name", Type.GetType("System.String"));
            oldData.Columns.Add("recharge_no", Type.GetType("System.String"));
            oldData.Columns.Add("payment", Type.GetType("System.String"));
            oldData.Columns.Add("amount", Type.GetType("System.String"));
            oldData.Columns.Add("add_time", Type.GetType("System.String"));
            oldData.Columns.Add("complete_time", Type.GetType("System.String"));
            if (resultList.TotalItems > 0)
            {
                foreach (var item in resultList.Items)
                {
                    DataRow row = oldData.NewRow();
                    row["id"] = item.id;
                    row["user_id"] = item.user_id;
                    row["status"] = item.status;
                    row["user_name"] = item.user_name;
                    row["recharge_no"] = item.recharge_no;
                    row["payment"] = new BLL.payment().GetTitle(item.payment_id);
                    row["amount"] = item.amount.ToString();
                    row["add_time"] = item.add_time.ToString();
                    row["complete_time"] = item.complete_time.ToString();
                    oldData.Rows.Add(row);
                }
            }
            return Json(new { status = 1, msg = "", item = oldData, totalCount = resultList.TotalItems });
        }
        #endregion
        #region 删除
        [HttpPost]
        [ChkAdminJson("user_recharge_log", DTEnums.ActionEnum.Delete)]
        public JsonResult RechargeDelete(List<int> ids)
        {
            return _service.RechargeDelete(ids);
        }
        #endregion
        #region 编辑、添加
        [ChkAdminScript("user_recharge_log", DTEnums.ActionEnum.View)]
        public ActionResult Recharge_Edit()
        {
            string username = DTRequest.GetQueryString("username");
            ViewBag.RechargeNo = Utils.GetOrderNumber(); //随机生成订单号
            ViewBag.UserName = username;
            ViewBag.paymenListItems = _service.OrderPaymentBind("type=1", "sort_id asc,id asc"); //绑定支付方式
            return View();
        }
        [HttpPost, CustomValidateAntiForgeryToken]
        [ChkAdminJson("user_recharge_log", DTEnums.ActionEnum.Add)]
        public ActionResult RechargeSave(Model.user_recharge t)
        {
            SaveModel save = _service.RechargeSave(t);
            save.url = save.status == 0 ? "back" : "../users/recharge_list";
            return Json(save);
        }
        #endregion
        #endregion
        #region 短信模板
        [ChkAdminScript("user_sms_template", DTEnums.ActionEnum.View)]
        public ActionResult Sms_Template_List()
        {
            return View();
        }
        #region 组合SQL查询语句==========================
        protected string SmsCombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and (title like  '%" + _keywords + "%' or content like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion
        #region 数据绑定
        [ChkAdminScript("user_sms_template", DTEnums.ActionEnum.View)]
        public JsonResult SmsList(string keywords, int page = 1, int limit = 10)
        {
            string strWhere = "id > 0" + SmsCombSqlTxt(keywords);
            string strOrder = "id asc";
            var resultList = _service.SmsBind(strWhere, strOrder, page, limit);
            return Json(new { status = 1, msg = "", item = resultList.Items, totalCount = resultList.TotalItems });
        }
        #endregion

        #region 删除
        [HttpPost]
        [ChkAdminJson("user_sms_template", DTEnums.ActionEnum.Delete)]
        public JsonResult SmsDelete(List<int> ids)
        {
            return _service.MailDelete(ids);
        }
        #endregion

        #region 编辑、添加
        [ChkAdminScript("user_sms_template", DTEnums.ActionEnum.View)]
        public ActionResult Sms_Template_Edit()
        {
            Model.sms_template model = new Model.sms_template();
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                _action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (_id == 0)
                {
                    return ResultMsg("传输参数不正确！", "back");
                }
                model = _service.SmsInfo(_id);
                if (model == null)
                {
                    return ResultMsg("记录不存在或已被删除！", "back");
                }
            }
            return View(model);
        }
        [HttpPost, ValidateInput(false), CustomValidateAntiForgeryToken]
        public ActionResult SmsTemplateSave(Model.sms_template t)
        {
            SaveModel save = _service.SmsSave(_action, _id, t);
            save.url = save.status == 0 ? "back" : "../users/sms_template_list";
            return Json(save);
        }
        #endregion
        #endregion
        #region 会员管理
        #region 页面加载
        [ChkAdminScript("user_sms", DTEnums.ActionEnum.View)]
        public ActionResult User_Sms()
        {
            List<SelectListItem> groupListItems = new List<SelectListItem>();
            groupListItems.Add(new SelectListItem() { Text = "请选择组别", Value = "" });
            groupListItems.AddRange(_service.GroupTreeBind("is_lock=0"));
            ViewBag.groupListItems = groupListItems;
            string mobiles = DTRequest.GetString("mobiles");
            if (!string.IsNullOrEmpty(mobiles))
            {
                ViewBag.mobiles = true;
                ViewBag.group = false;
                ViewBag.smsType = 1;
            }
            else
            {
                ViewBag.smsType = 2;
                ViewBag.mobiles = false;
                ViewBag.group = true;
            }
            ViewBag.mobile = mobiles;
            return View();
        }
        [ChkAdminScript("user_list", DTEnums.ActionEnum.View)]
        public ActionResult User_List()
        {
            return View();
        }
        [ChkAdminScript("user_email", DTEnums.ActionEnum.View)]
        public ActionResult User_Email()
        {
            List<SelectListItem> groupListItems = new List<SelectListItem>();
            groupListItems.Add(new SelectListItem() { Text = "请选择组别", Value = "" });
            groupListItems.AddRange(_service.GroupTreeBind("is_lock=0"));
            ViewBag.groupListItems = groupListItems;
            string emails = DTRequest.GetString("email");
            if (!string.IsNullOrEmpty(emails))
            {
                ViewBag.emails = true;
                ViewBag.group = false;
                ViewBag.emailType = 1;
            }
            else
            {
                ViewBag.emailType = 2;
                ViewBag.emails = false;
                ViewBag.group = true;
            }
            ViewBag.email = emails;
            return View();
        }
        [ChkAdminScript("user_list", DTEnums.ActionEnum.Edit)]
        public ActionResult User_Edit()
        {
            Model.users model = new Model.users();
            Model.user_login_log logModel = new Model.user_login_log();
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                if (_id == 0)
                {
                    return ResultMsg("传输参数不正确！", "back");
                }
                model = _service.UserInfo(_id);
                if (model == null)
                {
                    return ResultMsg("记录不存在或已被删除！", "back");
                }
                logModel = new BLL.user_login_log().GetModel("user_name='" + model.user_name + "'", "", "id desc");
                ViewBag.ClientScript = Jscript("$('#user_name').removeAttr('ajaxurl').attr('disabled','disabled');os.render();");
            }
            else
            {
                model.reg_time = DateTime.Now;
                model.reg_ip = DTRequest.GetIP();
                model.birthday = new DateTime(1900, 1, 1);
            }
            ViewBag.logModel = logModel;
            return View(model);
        }
        [ChkAdminScript("user_config", DTEnums.ActionEnum.View)]
        public ActionResult User_Config()
        {
            return View(_service.UserConfigInfo());
        }
        [ChkAdminScript("user_audit", DTEnums.ActionEnum.View)]
        public ActionResult User_Audit()
        {
            return View();
        }
        #endregion
        #region 保存提交
        //暂只用于修改余额 积分
        [HttpPost]
        [ChkAdminJson("user_list", DTEnums.ActionEnum.Edit)]
        public JsonResult UserFieldSave(string field, int value, int id = 0)
        {
            if (string.IsNullOrEmpty(field) || id == 0)
            {
                return Json(new { status = 0, msg = "参数错误！" });
            }
            BLL.users bll = new BLL.users();
            if (bll.UpdateFile("id=" + id, field + "=" + value))
            {
                return Json(new { status = 1, msg = "修改成功！" });
            }
            return Json(new { status = 0, msg = "修改失败！" });
        }

        [HttpPost]
        [ChkAdminJson("user_oauth", DTEnums.ActionEnum.Edit)]
        public JsonResult OauthAppFieldSave(string field, int value, int id = 0)
        {
            if (string.IsNullOrEmpty(field) || id == 0)
            {
                return Json(new { status = 0, msg = "参数错误！" });
            }
            BLL.oauth_app bll = new BLL.oauth_app();
            if (bll.UpdateFile("id=" + id, field + "=" + value))
            {
                return Json(new { status = 1, msg = "修改成功！" });
            }
            return Json(new { status = 0, msg = "修改失败！" });
        }

        [HttpPost, ValidateInput(false), CustomValidateAntiForgeryToken]
        [ChkAdminJson("user_sms", DTEnums.ActionEnum.Add)]
        public ActionResult SmsSave()
        {
            SaveModel save = _service.SmsSave();
            save.url = save.status == 0 ? "back" : "../users/user_sms";
            return Json(save);
        }

        [HttpPost, ValidateInput(false), CustomValidateAntiForgeryToken]
        [ChkAdminJson("user_email", DTEnums.ActionEnum.Add)]
        public ActionResult EmailSave()
        {
            SaveModel save = _service.EmailSave();
            save.url = save.status == 0 ? "back" : "../users/user_email";
            return Json(save);
        }

        [HttpPost, ValidateInput(false), CustomValidateAntiForgeryToken]
        public ActionResult UserSave(Model.users t)
        {
            SaveModel save = _service.UserSave(_action, _id, t);
            save.url = save.status == 0 ? "back" : "../users/user_list";
            return Json(save);
        }

        [HttpPost, ValidateInput(false), CustomValidateAntiForgeryToken]
        [ChkAdminJson("user_config", DTEnums.ActionEnum.Edit)]
        public ActionResult ConfigSave(Model.userconfig t)
        {
            SaveModel save = _service.UserConfigSave(t);
            save.url = save.status == 0 ? "back" : "../users/user_config";
            return Json(save);
        }
        #endregion
        #region 页面数据获取
        #region 组合SQL查询语句
        protected string UserSqlTxt(string _keywords, int _group_id, int _site_id)
        {
            StringBuilder strTemp = new StringBuilder();
            if (_site_id > 0)
            {
                strTemp.Append(" and site_id=" + _site_id);
            }
            if (_group_id > 0)
            {
                strTemp.Append(" and group_id=" + _group_id);
            }

            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and (user_name like '%" + _keywords + "%' or mobile like '%" + _keywords + "%' or email like '%" + _keywords + "%' or nick_name like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion
        #region 数据绑定
        [ChkAdminJson("user_list", DTEnums.ActionEnum.View)]
        public JsonResult UserList(string keywords, int group_id = 0, int page = 1, int site_id = 0, int limit = 10)
        {
            string strWhere = "id > 0" + UserSqlTxt(keywords, group_id, site_id);
            string strOrder = "reg_time desc,id desc";
            var resultList = _service.UserBind(strWhere, strOrder, page, limit);
            DataTable Items = DataHelper.ListToDataTable(resultList.Items);
            DataTable oldData = Items.Clone();
            if (resultList.TotalItems > 0)
            {
                oldData.Columns.Add("groupTitle", Type.GetType("System.String"));
                foreach (DataRow item in Items.Rows)
                {
                    DataRow row = oldData.NewRow();
                    row["user_name"] = item["user_name"];
                    row["email"] = item["email"];
                    row["mobile"] = item["mobile"];
                    row["amount"] = item["amount"];
                    row["status"] = item["status"];
                    row["reg_time"] = item["reg_time"];
                    row["avatar"] = item["avatar"];
                    row["nick_name"] = item["nick_name"];
                    row["point"] = item["point"];
                    row["id"] = item["id"];
                    row["groupTitle"] = new BLL.user_groups().GetTitle(Utils.ObjToInt(item["group_id"]));
                    oldData.Rows.Add(row);
                }
            }
            return Json(new { status = 1, msg = "", item = oldData, totalCount = resultList.TotalItems });
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string UserAuditSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and (user_name like '%" + _keywords + "%' or mobile like '%" + _keywords + "%' or email like '%" + _keywords + "%' or nick_name like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion
        #region 数据绑定
        [ChkAdminJson("user_audit", DTEnums.ActionEnum.View)]
        public JsonResult UserAuditList(string keywords, int page = 1, int limit = 10)
        {
            string strWhere = "status>0 and status<3" + UserAuditSqlTxt(keywords);
            string strOrder = "reg_time desc,id desc";
            var resultList = _service.UserBind(strWhere, strOrder, page, limit);
            DataTable Items = DataHelper.ListToDataTable(resultList.Items);
            DataTable oldData = Items.Clone();
            if (resultList.TotalItems > 0)
            {
                oldData.Columns.Add("groupTitle", Type.GetType("System.String"));
                foreach (DataRow item in Items.Rows)
                {
                    DataRow row = oldData.NewRow();
                    row["user_name"] = item["user_name"];
                    row["email"] = item["email"];
                    row["mobile"] = item["mobile"];
                    row["amount"] = item["amount"];
                    row["status"] = item["status"];
                    row["reg_time"] = item["reg_time"];
                    row["point"] = item["point"];
                    row["avatar"] = item["avatar"];
                    row["nick_name"] = item["nick_name"];
                    row["id"] = item["id"];
                    row["groupTitle"] = new BLL.user_groups().GetTitle(Utils.ObjToInt(item["group_id"]));
                    oldData.Rows.Add(row);
                }
            }
            return Json(new { status = 1, msg = "", item = oldData, totalCount = resultList.TotalItems });
        }
        #endregion

        #region 删除
        #region 删除
        [HttpPost]
        [ChkAdminJson("user_list", DTEnums.ActionEnum.Delete)]
        public JsonResult UserDelete(List<int> ids)
        {
            return _service.UserDelete(ids);
        }
        #endregion
        #endregion
        #region 审核
        [HttpPost]
        [ChkAdminJson("user_audit", DTEnums.ActionEnum.Audit)]
        public JsonResult UserAudit(List<int> ids)
        {
            return _service.UserAudit(ids);
        }
        #endregion
        #endregion
        #endregion
    }
}
